package com.liao.es.dao.impl;

import com.liao.es.dao.PhoneCustomDao;
import com.liao.es.dao.PhoneDao;
import com.liao.es.pojo.Shopping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;

import java.util.ArrayList;
import java.util.List;

public class PhoneDaoImpl implements PhoneCustomDao {

    @Autowired
    private ElasticsearchRestTemplate esRestTemplate;
    @Override
    public List<Shopping> customQuery() {
        Criteria criteria = new Criteria("name").is("小米").and("price").is(9999);
        CriteriaQuery criteriaQuery = new CriteriaQuery(criteria);
        SearchHits<Shopping> search = esRestTemplate.search(criteriaQuery, Shopping.class);
        List<Shopping> shops = new ArrayList<>();
        search.forEach(shoppingSearchHit -> {
            shops.add(shoppingSearchHit.getContent());
        });
        return shops;
    }
}
